package com.housing.service.impl;

import com.zmetaport.common.core.utils.MapstructUtils;
import com.zmetaport.common.core.utils.StringUtils;
import com.zmetaport.common.mybatis.core.page.TableDataInfo;
import com.zmetaport.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.housing.domain.bo.OwnerBo;
import com.housing.domain.vo.OwnerVo;
import com.housing.domain.Owner;
import com.housing.mapper.OwnerMapper;
import com.housing.service.IOwnerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author chensm
 * @date 2024-12-15
 */
@RequiredArgsConstructor
@Service
public class OwnerServiceImpl implements IOwnerService {

    private final OwnerMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public OwnerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<OwnerVo> queryPageList(OwnerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Owner> lqw = buildQueryWrapper(bo);
        Page<OwnerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<OwnerVo> queryList(OwnerBo bo) {
        LambdaQueryWrapper<Owner> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Owner> buildQueryWrapper(OwnerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Owner> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Owner::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), Owner::getPhone, bo.getPhone());
        lqw.eq(bo.getDocumentType() != null, Owner::getDocumentType, bo.getDocumentType());
        lqw.eq(StringUtils.isNotBlank(bo.getDocumentNumber()), Owner::getDocumentNumber, bo.getDocumentNumber());
        lqw.eq(bo.getCreateTime() != null, Owner::getCreateTime, bo.getCreateTime());
        lqw.eq(bo.getUpdateTime() != null, Owner::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(OwnerBo bo) {
        Owner add = MapstructUtils.convert(bo, Owner.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(OwnerBo bo) {
        Owner update = MapstructUtils.convert(bo, Owner.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Owner entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
